package com.frontend.screens;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.data.documents.Appointment;
import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.users.User;

/**
 * 
 * @author Elizabeth Jizzle
 * @author Taylor Wrobel
 * 
 */
public class NavigationScreen extends Screen {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5171168216249044768L;
	private JLabel testLabel;
	private JPanel b1, b2, b3, b4, b5, b6;
	private JButton createUser, viewAppt, viewInv, search, chPass, createAppt,
			logout;
	private NavigationScreen superClass;

	public NavigationScreen(DoctorsOfficeClient window) {
		super(window);

		ButtonListener bl = new ButtonListener();

		superClass = this;

		createUser = new JButton("New User");
		createAppt = new JButton("Request an Appointment");
		search = new JButton("Search for a User");
		chPass = new JButton("Change your Password");
		viewAppt = new JButton("View your Appointments");
		viewInv = new JButton("View your Invoices");

		logout = new JButton("Logout");

		createUser.setOpaque(false);
		createAppt.setOpaque(false);
		search.setOpaque(false);
		chPass.setOpaque(false);
		viewAppt.setOpaque(false);
		viewInv.setOpaque(false);

		logout.setOpaque(false);

		createUser.addActionListener(bl);
		createAppt.addActionListener(bl);
		search.addActionListener(bl);
		chPass.addActionListener(bl);
		viewAppt.addActionListener(bl);
		viewInv.addActionListener(bl);

		logout.addActionListener(bl);

		logout.setAlignmentX(CENTER_ALIGNMENT);

		b1 = new JPanel();
		b1.setOpaque(false);

		testLabel = new JLabel("Welcome, " + window.getCurrentUser().getName());
		testLabel.setAlignmentX(CENTER_ALIGNMENT);

		b2 = new JPanel();
		b2.setOpaque(false);

		b3 = new JPanel();
		b3.setOpaque(false);

		b4 = new JPanel();
		b4.setOpaque(false);
		b3.add(b4);

		if (window.getCurrentUser().getUser_type() == User.PATIENT) {
			b4.setLayout(new GridLayout(7, 1));
			b4.add(createAppt);
			JPanel p1 = new JPanel();
			p1.setOpaque(false);
			b4.add(p1);
			b4.add(chPass);
			JPanel p2 = new JPanel();
			p2.setOpaque(false);
			b4.add(p2);
			b4.add(viewAppt);
			JPanel p3 = new JPanel();
			p3.setOpaque(false);
			b4.add(p3);
			b4.add(viewInv);
		} else {
			b4.setLayout(new GridLayout(5, 1));
			b4.add(createUser);
			JPanel p1 = new JPanel();
			p1.setOpaque(false);
			b4.add(p1);
			b4.add(search);
			JPanel p2 = new JPanel();
			p2.setOpaque(false);
			b4.add(p2);
			b4.add(chPass);
		}

		b5 = new JPanel();
		b5.setOpaque(false);

		b6 = new JPanel();
		b6.setOpaque(false);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		add(b1);
		add(testLabel);
		add(b2);
		add(b3);
		add(b4);
		add(b5);
		add(logout);
		add(b6);

	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			testLabel.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));

			if (window.getCurrentUser().getUser_type() == User.PATIENT) {
				b4.setMaximumSize(new Dimension(getWidth() / 3, getHeight() / 2));
				b4.setPreferredSize(new Dimension(getWidth() / 3,
						getHeight() / 2));
				logout.setSize(new Dimension(getWidth() / 8, getHeight() / 20));
				logout.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));

				createUser.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 30));
				createAppt.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 30));
				viewAppt.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 30));
				viewInv.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 30));
				search.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
				chPass.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			} else {
				b4.setMaximumSize(new Dimension(getWidth() * 2 / 3,
						getHeight() / 2));
				b4.setPreferredSize(new Dimension(getWidth() * 2 / 3,
						getHeight() / 2));
				logout.setSize(new Dimension(getWidth() / 8, getHeight() / 20));
				logout.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));

				createUser.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 30));
				createAppt.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 30));
				search.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
				chPass.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			}

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	private class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(logout)) {
				window.logout();
			} else if (e.getSource().equals(chPass)) {
				window.changeScreen(new ChangePasswordScreen(window, window
						.getCurrentUser().getUsername(), superClass));
			} else if (e.getSource().equals(search)) {
				window.changeScreen(new SearchScreen(window));
			} else if (e.getSource().equals(createUser)) {
				window.changeScreen(new CreateUserScreen(window));
			} else if (e.getSource().equals(createAppt)) {
				try {
					window.changeScreen(new AppointmentScreen(window,
							new Appointment(window.getNet().generateDocId()),
							window.getCurrentUser(), window.getCurrentScreen()));
				} catch (ConnectionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} else if (e.getSource().equals(viewAppt)) {
				window.changeScreen(new ListAppointmentsScreen(window,
						superClass));
			} else if (e.getSource().equals(viewInv)) {
				window.changeScreen(new ListInvoicesScreen(window, superClass));
			}
		}

	}
}
